package beerAgent;

import genotype.BeerGenotype;
import gui.GameModel;
import gui.RunSettingPanel;

import java.awt.GridBagConstraints;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JTextArea;

public class RunBeerSettingPanel extends RunSettingPanel {

	private static final long serialVersionUID = 1L;
	private JTextArea textField;
	private JButton addPhenotypeButton;

	public RunBeerSettingPanel(BeerGameModel gameModel) {
		super(gameModel);
//
//		addPhenotypeButton = new JButton();
//		addPhenotypeButton.setText("Add phenotype");
//		addPhenotypeButton.addActionListener(new AddPhenotypeListner());
//		addComponent(addPhenotypeButton, 2, 0);
//
//		addTextField();
	}

	private void addTextField() {
		textField = new JTextArea();
		textField.setEditable(true);
		textField.setRows(4);
		textField.setLineWrap(true);
		textField.setSize(750, 75);

		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 3;
		add(textField, constraints);
		// gameModel.pack();
	}

	public void setPhenotype(BeerPhenotype phenotype) {
//		textField.setText(Arrays.toString(((BeerGenotype) phenotype
//				.getGenotype()).getArrayGenotype()));
	}

	private class AddPhenotypeListner implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String input = textField.getText();
			if (input == null || input.equals("")) {
				return;
			}
			BeerGenotype genotype = new BeerGenotype(getWeights(input),
					((BeerGameModel) gameModel).getParameters());
			BeerPhenotype phenotype = (BeerPhenotype) genotype.evolveGenotype(); 
			phenotype.externalPhenotypeSetup(3); 
			gameModel.addPheontype(phenotype, -1);
		}

		private double[] getWeights(String input) {
			input = input.substring(1, input.length() - 2);
			String[] stringArray = input.split(",");
			double[] doubleArray = new double[stringArray.length];
			for (int i = 0; i < stringArray.length; i++) {
				doubleArray[i] = Double.parseDouble(stringArray[i].trim());
			}
			return doubleArray;
		}

	}

}
